package sortAlgorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSON;

public class Test {

    public static void main(String[] args) {

        // int[][] intervals = new int[][] {
        //         { 1, 3 },
        //         { 2, 6 },
        //         { 8, 10 },
        //         { 15, 18 }
        // };
        //
        // int[][] merged = merge(intervals);
        //
        // System.out.println("merged: "  + JSON.toJSONString(merged));

        System.out.println(new Date(1705766451000L));

    }

    public static int[][] merge(int[][] intervals) {

        if (intervals == null || intervals.length == 0) {
            return null;
        }

        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return Integer.compare(o1[0], o2[0]);
            }
        });


        System.out.println("sorted: "  + JSON.toJSONString(intervals));


        int left = intervals[0][0], right = intervals[0][1];

        List<List<Integer>> mergeList = new ArrayList<>();

        for (int i = 1; i < intervals.length; i++) {

            if (intervals[i][0] > right) {
                List<Integer> list = new ArrayList<>();
                list.add(left);
                list.add(right);
                mergeList.add(list);

                left = intervals[i][0];
                right = intervals[i][1];
                continue;
            }

            if (intervals[i][1] > right) {
                right = intervals[i][1];
            }

        }
        // 最后一个区间的元素 加上去
        List<Integer> list = new ArrayList<>();
        list.add(left);
        list.add(right);
        mergeList.add(list);

        int[][] mergeIntervals = new int[mergeList.size()][2];

        for (int i = 0; i < mergeList.size(); i++) {
            List<Integer> interval = mergeList.get(i);
            mergeIntervals[i][0] = interval.get(0);
            mergeIntervals[i][1] = interval.get(1);
        }

        return mergeIntervals;
    }

}
